using UnityEngine;

public class CameraController : Instance<CameraController>
{
	public Camera cameraMain;

	private float speed;

	private Vector3 tem;

	private float tem2;

	public static float timeMoveCamera = 0.8f;

	private Transform transCamera;

	private new void Awake()
	{
		base.Awake();
		transCamera = cameraMain.transform;
	}

	private void Start()
	{
		cameraMain.aspect = 1.66666663f;
	}

	private void FixedUpdate()
	{
		Vector3 position = Instance<Ball>.instance.getPosition();
		if (position.x < -7f)
		{
			Vector3 position2 = Instance<Ball>.instance.getPosition();
			if (position2.z > 13f)
			{
				transCamera.rotation = Quaternion.Euler(Vector3.SmoothDamp(transCamera.eulerAngles, MathHelper.setY(transCamera.eulerAngles, 55f), ref tem, timeMoveCamera));
				return;
			}
			Vector3 position3 = Instance<Ball>.instance.getPosition();
			if (position3.z < -13f)
			{
				transCamera.rotation = Quaternion.Euler(Vector3.SmoothDamp(transCamera.eulerAngles, MathHelper.setY(transCamera.eulerAngles, 125f), ref tem, timeMoveCamera));
				return;
			}
		}
		if ((Instance<Court>.instance.quarter_timeRemaining <= 0f && !Instance<Ball>.instance.isShooting) || Instance<Ball>.instance.isInState(BallState_Dribble_Stay.getInstance()))
		{
			return;
		}
		Vector3 localPosition = transCamera.localPosition;
		if (Mathf.Abs(localPosition.z) <= Global.cameraPos_Left.z)
		{
			Vector3 localPosition2 = transCamera.localPosition;
			float z = localPosition2.z;
			Vector3 position4 = Instance<Ball>.instance.getPosition();
			if (Mathf.Abs(z - position4.z) > 0.2f && !Instance<Ball>.instance.isInState(BallState_Dribble_Stay.getInstance()))
			{
				if (Instance<Ball>.instance.isInState(BallState_Pass.getInstance()) || Instance<Ball>.instance.isInState(BallState_Shoot.getInstance()))
				{
					if (speed < Global.MoveSpeed_Camera)
					{
						speed = Global.MoveSpeed_Camera;
					}
					speed += 0.15f;
				}
				else
				{
					speed = Global.MoveSpeed_Camera;
				}
				Transform transform = transCamera;
				Vector3 localPosition3 = transCamera.localPosition;
				Vector3 localPosition4 = transCamera.localPosition;
				Vector3 position5 = Instance<Ball>.instance.getPosition();
				transform.localPosition = Vector3.Lerp(localPosition3, MathHelper.setZ(localPosition4, position5.z), Time.fixedDeltaTime * speed);
				Vector3 localPosition5 = transCamera.localPosition;
				if (localPosition5.z > Global.cameraPos_Left.z)
				{
					transCamera.localPosition = Global.cameraPos_Left;
				}
				else
				{
					Vector3 localPosition6 = transCamera.localPosition;
					if (localPosition6.z < Global.cameraPos_Right.z)
					{
						transCamera.localPosition = Global.cameraPos_Right;
					}
				}
			}
		}
		Vector3 position6 = Instance<Ball>.instance.getPosition();
		if (position6.z > 0f)
		{
			Vector3 position7 = Instance<Ball>.instance.getPosition();
			if (position7.z > 3f)
			{
				Vector3 position8 = Instance<Ball>.instance.getPosition();
				int value = (int)((position8.z - 3f) * 2f * 0.333f);
				value = Mathf.Clamp(value, 0, 4);
				transCamera.rotation = Quaternion.Euler(Vector3.SmoothDamp(transCamera.eulerAngles, MathHelper.setY(transCamera.eulerAngles, 80 - value * 5), ref tem, timeMoveCamera));
				cameraMain.fieldOfView = Mathf.SmoothDamp(cameraMain.fieldOfView, 30f, ref tem2, timeMoveCamera);
			}
			else
			{
				transCamera.rotation = Quaternion.Euler(Vector3.SmoothDamp(transCamera.eulerAngles, MathHelper.setY(transCamera.eulerAngles, 90f), ref tem, timeMoveCamera));
				cameraMain.fieldOfView = Mathf.SmoothDamp(cameraMain.fieldOfView, 35f, ref tem2, timeMoveCamera);
			}
		}
		else
		{
			Vector3 position9 = Instance<Ball>.instance.getPosition();
			if (position9.z < -3f)
			{
				Vector3 position10 = Instance<Ball>.instance.getPosition();
				int value2 = (int)((0f - position10.z - 3f) * 2f * 0.333f);
				value2 = Mathf.Clamp(value2, 0, 4);
				transCamera.rotation = Quaternion.Euler(Vector3.SmoothDamp(transCamera.eulerAngles, MathHelper.setY(transCamera.eulerAngles, 100 + value2 * 5), ref tem, timeMoveCamera));
				cameraMain.fieldOfView = Mathf.SmoothDamp(cameraMain.fieldOfView, 30f, ref tem2, timeMoveCamera);
			}
			else
			{
				transCamera.rotation = Quaternion.Euler(Vector3.SmoothDamp(transCamera.eulerAngles, MathHelper.setY(transCamera.eulerAngles, 90f), ref tem, timeMoveCamera));
				cameraMain.fieldOfView = Mathf.SmoothDamp(cameraMain.fieldOfView, 35f, ref tem2, timeMoveCamera);
			}
		}
		if (Instance<Ball>.instance.isInState(BallState_Shoot.getInstance()))
		{
			if (Instance<Ball>.instance.isDunk || Instance<Ball>.instance.isLayUp)
			{
				cameraMain.fieldOfView = Mathf.SmoothDamp(cameraMain.fieldOfView, 20f, ref tem2, timeMoveCamera);
				transCamera.rotation = Quaternion.Euler(Vector3.SmoothDamp(transCamera.eulerAngles, MathHelper.setX(transCamera.eulerAngles, 18f), ref tem, timeMoveCamera));
			}
			else if (Instance<Ball>.instance.isShooting)
			{
				transCamera.rotation = Quaternion.Euler(Vector3.SmoothDamp(transCamera.eulerAngles, MathHelper.setX(transCamera.eulerAngles, 16f), ref tem, timeMoveCamera));
			}
		}
		else
		{
			Vector3 position11 = Instance<Ball>.instance.getPosition();
			if (position11.x > -6f)
			{
				transCamera.rotation = Quaternion.Euler(Vector3.SmoothDamp(transCamera.eulerAngles, MathHelper.setX(transCamera.eulerAngles, 20f), ref tem, timeMoveCamera));
			}
			else
			{
				transCamera.rotation = Quaternion.Euler(Vector3.SmoothDamp(transCamera.eulerAngles, MathHelper.setX(transCamera.eulerAngles, 24f), ref tem, timeMoveCamera));
			}
		}
	}

	public Vector3 getCameraPos()
	{
		return transCamera.localPosition;
	}
}
